<?php
namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Document;
use App\Models\DocumentArticle;
use App\Models\Category;
use App\Models\Goods;
use App\Models\CollectArticle;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    public function __construct(Request $request)
    {
        $this->file = storage_path('framework/views/') . md5($request->fullUrl()) . '.html';
    }

    public function index(Request $request)
    {

        if (is_file($this->file) && cache_check($this->file, 0.5)) {
            readfile($this->file);
        } else {

            $objDocument = new Document();
            $data = $objDocument->getArticleData();

            $data['list20'] = $objDocument->getArticleRand(20);
            $objGoods = new Goods();

            $data['goodsCate'] = $objGoods->getCategoryArray();
            
            $data['Goods'] = $objGoods->getGoodsData();
            dd($data);
            // $res = view('home.article.index' . mt_rand(1, 10), $data);
            $res = view('home.article.index', $data);
            // save last webPage content
            if (!config('app.debug')) file_put_contents($this->file, $res);
            return $res;
        }
    }

    public function lists(Request $request)
    {
        if (is_file($this->file) && cache_check($this->file, 0.1)) {
            readfile($this->file);
        } else {
            $objDocument = new Document();
            //Rand document
            $data = $objDocument->getArticleData();

            // dynamic page
            $data['PageCount'] = $objDocument->getListsCount($request->id);
            $data['PageMax'] = ceil($data['PageCount'] / 10);
            $data['currentUrl'] = url('lists/' . $request->id);
            $data['currentPage'] = $request->page ? $request->page : 1;

            //current cate document
            $data['lists'] = $objDocument->getLists($request->id, (int)$data['currentPage'] - 1);
            $data['info']['title'] = Category::where('id', $request->id)->value('title');

            $objGoods = new Goods();
            $data['Goods'] = $objGoods->getGoodsData();
            $res = view('home.article.list', $data);
            // save last webPage content
            if (!config('app.debug')) file_put_contents($this->file, $res);
            return $res;
        }
    }

    public function detail(Request $request)
    {
        if (is_file($this->file) && cache_check($this->file, 0.1)) {
            readfile($this->file);
        } else {
            $objDocument = new Document();
            $data = $objDocument->getArticleData();
            $objDocumentArticle = new DocumentArticle();
            $data['info'] = $objDocumentArticle->getInfo($request->id);
            $objGoods = new Goods();
            $data['Goods'] = $objGoods->getGoodsData();
            $res = view('home.article.detail', $data);
            // save last webPage content
            if (!config('app.debug')) file_put_contents($this->file, $res);
            return $res;
        }

    }

    public function search($id = 1)
    {
        echo 'welcome';
        exit();
        $objDocument = new Document();
        $data = $objDocument->getArticleData();

        $objDocumentArticle = new DocumentArticle();
        $data['info'] = $objDocumentArticle->getInfo($id);
        return view('home.article.search', $data);
    }
}
